<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Api\Service;
/**
 * @todo 对内api
 * @author PHP@妖孽 <dino_ma@163.com>
 * @since 2015-9-3
 */
class SmsService{
	
	 protected $error            =   '';
	
	
	/**
	 * @todo 发送短信
	 * @param number $type 0,螺丝帽 其他待定
	 */
	 public function sendSms($mobile_phone,$sms_content,$type=0){
		if(!preg_match('/^1[3-8][0-9]{9}$/', $mobile_phone)){
			$this->error ='手机号码有问题';
			return false;
		}
		if(strlen($sms_content)>60 && strlen($sms_content) <1){
			$this->error ='短信长度有问题~';
			return false;
		}
		$type = (int)$type;
		switch($type){
			case 0:
				//短信发送实现
			    $this->luoSiMaoSendSms($mobile_phone, $sms_content);
			    
				break;
			default:
				$this->error ='短信发送方式有错误~';
				return false;
				break;
			
		}
	}
	
	/**
	 * @todo 螺丝猫发送短信实现
	 */
	private function luoSiMaoSendSms($mobile_phone,$sms_content){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");
		curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-11c0b7dc2860abb7885c70752ed010d2');
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $mobile_phone,'message' => $sms_content.'【家家户户网】'));
		$res = curl_exec( $ch );
		curl_close( $ch );
// 		$res  = curl_error( $ch );
		$call_back = json_decode($res,true);
		if($call_back['error']==0 && $call_back['msg']=='ok'){
			\Think\Log::write('手机号:'.$mobile_phone.'短信内容:'.$sms_content.'短信发送成功');
// 			return true;
		}else{
// 			$this->error=$call_back['msg'];
			\Think\Log::write('手机号:'.$mobile_phone.'短信内容:'.$sms_content.'短信发送失败,错误为:'.$this->luoSiMaoError($call_back['error']));
// 			return false;
		}
		
	}
	
	/**
	 * @todo 螺丝帽错误码
	 */
	private function luoSiMaoError($error_num){
		switch ($error_num){
			case 10:
				return '验证信息失败 检查api key是否正确，编码格式是否正确';
				break;
			case 20:
				return  '短信余额不足 进入个人中心购买充值';
				break;
			case 30:
				return  '短信内容为空 检查调用传入参数：message';
				break;
			case -31:
				return  '短信内容存在敏感词 修改短信内容，更换词语';
				break;
			case 32:
				return  '短信内容缺少签名信息 短信内容末尾增加签名信息，格式为：【公司名称】';
				break;
			case 40:
				return  '错误的手机号 检查手机号是否正确';
				break;
			case 41:
				return  '号码在黑名单中 号码因频繁发送或其他原因暂停发送，请联系客服确认';
				break;
			case 42:
				return  '验证码类短信发送频率过快 前台增加60秒获取限制';
				break;
			case 50:
				return  '请求发送IP不在白名单内 查看触发短信IP白名单的设置';
				break;
		}
	}
	
	/**
	 * @todo 返回最后一次错误记录
	 * @return string
	 */
	public function getError(){
		return $this->error;
	}
}
